<template>
  <a-drawer
    :title="title"
    placement="right"
    :closable="true"
    :width="width"
    :visible="visible"
    @close="handleCancel"
    destroyOnClose
  >
    <a-spin :spinning="confirmLoading">
      <j-form-container :disabled="formDisabled">
        <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
          <a-row>
            <a-col :span="24">
              <a-form-model-item label="工种类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="kindOfWorkType">
                <a-select
                  show-search
                  :allowClear="true"
                  placeholder="请选择工种类型"
                  option-filter-prop="children"
                  v-model="model.kindOfWorkType"
                  style="width: 100%"
                  :filter-option="filterOptionKindOfWorkType"
                  @change="changeKindOfWorkType">
                  <a-select-option :value="item.id" v-for="(item,index) in kindOfWorkTypeData" :key="index">
                    {{item.kindOfWork}}
                  </a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="施工部位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="constructionPlace">
                <a-input v-model="model.constructionPlace" placeholder="请输入施工部位" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="specifications">
                <a-input v-model="model.specifications" placeholder="请输入规格" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="工价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="labourCost">
                <a-input-number v-model="model.labourCost" placeholder="请输入工价" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unit">
                <a-input v-model="model.unit" placeholder="请输入单位"  ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
                <a-textarea
                  v-model="model.remark"
                  placeholder="请输入备注"
                  :auto-size="{ minRows: 3, maxRows: 5 }"/>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </j-form-container>
    </a-spin>
    <a-button type="primary" style="width: 100%" @click="handleOk">保存</a-button>

  </a-drawer>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'addPriceManagementModal',
    components: {

    },
    data () {
      return {
        title:'',
        width:'100%',
        visible: false,
        disableSubmit: false,
        kindOfWorkTypeData:[],
        model:{
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          kindOfWorkType: [{ required: true, message: '请选择工种' }],
          labourCost: [
            { required: true, message: '请输入工价' },
            {"pattern": /^\d+(?=\.{0,1}\d+$|$)/,"message": "只能输入正数"}
          ],
          unit: [{ required: true, message: '请输入单位' }],
          constructionPlace: [{ required: true, message: '请输入施工部位' }]
        },
        url: {
          add: "/ghPriceManagement/add",
          edit: "/ghPriceManagement/edit",
          queryById: "/ghPriceManagement/queryById",
          queryKindOfWorkTypeList: "/ghKindOfWork/queryList"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
      //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.loadKindOfWorkTypeData();
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      close () {
        this.model = {};
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              this.close();
            })
          }

        })
      },
      //加载工种类型数据
      loadKindOfWorkTypeData(){
        getAction(this.url.queryKindOfWorkTypeList).then((res) => {
          if (res.success) {
            this.kindOfWorkTypeData = res.result.records||res.result;
          }else{
            this.$message.warning(res.message)
          }
        })
      },
      handleCancel () {
        this.close()
      },
      filterOptionKindOfWorkType(input, option){
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      changeKindOfWorkType(value){

      },
    }
  }
</script>